

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../models/http_models.dart';
import '../../models/json_convert_content.dart';


extension ResponseConverT on ResponseBodyMt {
  T? converterT<T>() {
    return MyJsonConverter.fromJsonAsT<T>(data);
  }
}

extension CancelTokenX on Ref {
  CancelToken cancelToken({VoidCallback? callback}) {
    final cancelToken = CancelToken();
    callback?.call();
    onDispose(cancelToken.cancel);
    return cancelToken;
  }
}

extension MapStringToList on Map<String, dynamic> {
  List<T> toListByKey<T>(String key) {
    final value = this[key];
    if(value==null) return [];
    return MyJsonConverter.convertListNotNull<T>(value);
  }
}

extension BuildContextExtension on BuildContext {

}


extension ListExtension<T> on List<T> {
  void pad(int length, T value) {
    if (length > this.length) {
      addAll(List.filled(length - this.length, value));
    }
  }
}

extension SubZeroAndDot on String {
  String subZeroAndDot() {
    var s = this;
    if (s.indexOf(".") > 0) {
      s = s.replaceAll(RegExp(r"0+?$"), ""); // 去掉多余的0
      s = s.replaceAll(RegExp(r"[.]$"), ""); // 如果最后一位是.则去掉
    }
    return s;
  }
}

extension GenericExt<T> on T {
  R let<R>(R Function(T t) transform) => transform(this);

  R? safeCast<R>() => this is R ? (this as R) : null;
}
